﻿package com.anxin.struts.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anxin.bean.Project;
import com.anxin.bean.Type;
import com.anxin.service.ProjectService;
import com.anxin.service.TypeService;

@Controller
@Scope("prototype")
public class ProjectAction extends BaseAction {

	private static final long serialVersionUID = -6637516549216513535L;
	private ProjectService service;
	private Project proj;

	private List<Type> types;
	private List<Type> flags;

	@Autowired
	public void setProjectService(ProjectService service) {
		this.service = service;
	}

	@Autowired
	private TypeService typeService;

	// 转到增加页面
	public String toAdd() {
		types = typeService.findTypeByParent(3);
		flags = typeService.findTypeByParent(7);
		return "add";
	}

	// 添加项目
	public String add() {
		service.save(proj);
		return "success";
	}

	// 转到修页面
	public String toUpdate() {
		proj = service.findById(proj.getId());
		return "update";
	}

	// 更新信息
	public String update() {
		Project temp = service.findById(proj.getId());
		temp.setId(proj.getId());
		temp.setName(proj.getName());
		temp.setProj_code(proj.getProj_code());

		service.update(temp);
		return "success";
	}

	// 查询列表
	public String query() {
		Map<String, String> param = new HashMap<String, String>();
		if (!"".equals(condition)) {
			if (querytype == 1) {
				param.put("name", condition);
			} else if (querytype == 2) {
				param.put("proj_code", condition);
			}
		}
		pageListData = service.queryPage(param, currentPage, pageSize);
		return "query";
	}

	// 删除项目
	public String delete() {
		service.delete(proj.getId());
		return "success";
	}

	public Project getProj() {
		return proj;
	}

	public void setProj(Project proj) {
		this.proj = proj;
	}

	public List<Type> getTypes() {
		return types;
	}

	public void setTypes(List<Type> types) {
		this.types = types;
	}

	public List<Type> getFlags() {
		return flags;
	}

	public void setFlags(List<Type> flags) {
		this.flags = flags;
	}

}
